home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DELPHI / SQLBUILD.ZIP / SQLDEMO.ZIP / DEMOMAIN.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-25  |  5.5 KB  |  192 lines

  1. unit Demomain;
  2.  
  3. interface
  4.  
  5. {Demonstration program using TSQLBuilder component}
  6. uses
  7.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  8.   Forms, Dialogs, Buttons, StdCtrls, DB, DBTables, SQLBuild, Grids, DBGrids,
  9.   ExtCtrls;
  10.  
  11. Const
  12.   MaxDatabases = 4; {Set to whatever you like}
  13. type
  14.   TForm1 = class(TForm)
  15.     Memo1: TMemo;
  16.     sbBuildSQL: TSpeedButton;
  17.     cboxChangeAlias: TCheckBox;
  18.     cboxUpdateQueries: TCheckBox;
  19.     cboxDeleteQueries: TCheckBox;
  20.     Query1: TQuery;
  21.     DBGrid1: TDBGrid;
  22.     DataSource1: TDataSource;
  23.     Bevel1: TBevel;
  24.     Label2: TLabel;
  25.     Label3: TLabel;
  26.     SQLBuilder1: TSQLBuilder;
  27.     cboxShowSysTables: TCheckBox;
  28.     cboxCanModify: TCheckBox;
  29.     cboxAutoGroupby: TCheckBox;
  30.     Label4: TLabel;
  31.     SpeedButton1: TSpeedButton;
  32.     cbSQLStyle: TComboBox;
  33.     Label5: TLabel;
  34.     rbQueryStartup: TRadioGroup;
  35.     procedure sbBuildSQLClick(Sender: TObject);
  36.     procedure FormCreate(Sender: TObject);
  37.     procedure FormDestroy(Sender: TObject);
  38.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  39.     procedure SpeedButton1Click(Sender: TObject);
  40.   private
  41.     { Private declarations }
  42.     DBArray : Array[1..(MaxDatabases-1)] of TDatabase;
  43.     StandardDB : TDatabase;
  44.     Procedure SetBuilderOptions;
  45.     Procedure PrepareAndRunQuery;
  46.   public
  47.     { Public declarations }
  48.   end;
  49.  
  50. var
  51.   Form1: TForm1;
  52.  
  53. implementation
  54. Uses
  55.   SqlBld08;
  56. {$R *.DFM}
  57.  
  58. function Long2Str(L : LongInt) : string;
  59.   {-Convert a long/word/integer/byte/shortint to a string}
  60. var
  61.   S : string;
  62. begin
  63.   Str(L, S);
  64.   Long2Str := S;
  65. end;
  66.  
  67. Procedure Tform1.SetBuilderOptions;
  68. Var
  69.   TheOptions:TSQLBuilderOptions;
  70. begin
  71.   TheOptions := [];
  72.   If cboxChangeAlias.Checked then
  73.     Include(TheOptions, sbAllowChangeAlias);
  74.   If cboxUpdateQueries.Checked then
  75.     Include(TheOptions, sbAllowUpdates);
  76.   If cboxDeleteQueries.Checked then
  77.     Include(TheOptions, sbAllowDeletes);
  78.   If cboxShowSysTables.Checked then
  79.     Include(TheOptions, sbShowSystemTables);
  80.   If cboxCanModify.Checked then
  81.     Include(TheOptions, sbEnableEditor);
  82.   SQLBuilder1.Options := TheOptions;
  83.   SQLBuilder1.AutoGroup := cboxAutoGroupby.Checked;
  84.   SQLBuilder1.UseWizardOnShow := (rbQueryStartup.ItemIndex = 1);
  85.   SQLBuilder1.AutoloadLastQuery := (rbQueryStartup.ItemIndex = 0);;
  86.   SQLBuilder1.SQLType := TSQLType(cbSQLStyle.ItemIndex);
  87. end;
  88.  
  89. procedure Tform1.PrepareAndRunQuery;
  90. {Opens any necessary databases, sets up the TQuery and runs it}
  91. Var
  92.   ACount     :Integer;
  93. begin
  94.   If SQLBuilder1.QueryType <> SelectQuery then
  95.     MessageDlg('Sorry, for safety reasons the demonstration program will only run "Select" queries.', mtInformation, [mbOk], 0)
  96.   else If SQLBuilder1.UsedAliasList.Count > MaxDatabases then
  97.     MessageDlg('Unable to run this query. The demo progam is limited to '
  98.                +'a maximum of '+Long2Str(MaxDatabases)+' separate databases', mtInformation, [mbOk], 0)
  99.   else
  100.   Try
  101.     screen.Cursor := crHourglass;
  102.  
  103.     {Place first alias in TQuery.Databasename}
  104.     If SQLBuilder1.IsHeterogeneous then
  105.       {use dummy program STANDARD alias as Databasename}
  106.       Query1.DatabaseName := StandardDB.Databasename
  107.     else
  108.       Query1.DatabaseName := SQLBuilder1.UsedAliasList.Strings[0];
  109.  
  110.     {If there is more than one alias involved then open databases for them}
  111.     If SQLBuilder1.IsMultiAlias then
  112.     For aCount := 1 to SQLBuilder1.UsedAliasList.Count-1 do
  113.     Try
  114.       DBArray[aCount].Free;
  115.       DBArray[aCount] := TDatabase.Create(Self);
  116.       DBArray[aCount].Aliasname := SQLBuilder1.UsedAliasList.Strings[aCount];
  117.       DBArray[aCount].Databasename := SQLBuilder1.UsedAliasList.Strings[aCount];
  118.       DBArray[aCount].Name := 'DB'+Long2Str(aCount+1);
  119.       DBArray[aCount].Open;
  120.     except
  121.       On E: EDatabaseError do
  122.       begin
  123.         MessageDlg('Unable to open the database: '+DBArray[aCount].Aliasname
  124.                    +#13#10+'Error: '+E.Message, mtError, [mbOk], 0);
  125.         abort;
  126.       end;
  127.     end;
  128.  
  129.     {If we get this far then attempt to run the query}
  130.     If SQLBuilder1.SQLModified then
  131.       Query1.SQL.Assign(SQLBuilder1.UserSQL)
  132.     else
  133.       Query1.SQL.Assign(SQLBuilder1.SQL);
  134.     Query1.Open;
  135.   finally
  136.     screen.Cursor := crDefault;
  137.   end;
  138. end;
  139.  
  140. procedure TForm1.FormCreate(Sender: TObject);
  141. var
  142.   dCount:Integer;
  143. begin
  144.   {Initialize the Database array}
  145.   For dCount := 1 to (MaxDatabases-1) do
  146.     DBArray[dCount] := Nil;
  147.   {Create a STANDARD type database alias to use with heterogeneous queries}
  148.   StandardDB := TDatabase.Create(Self);
  149.   StandardDB.Databasename := 'SQLBldDemo';
  150.   StandardDB.DriverName:= 'STANDARD';
  151.   StandardDB.Params.Clear;
  152.   StandardDB.Params.Add('PATH='+SQLBuilder1.PrivateDir);
  153.   StandardDB.Open;
  154.   cbSQLStyle.ItemIndex := 3;
  155. end;
  156.  
  157. procedure TForm1.FormDestroy(Sender: TObject);
  158. Var
  159.   dCount:Integer;
  160. begin
  161.   StandardDB.Free;
  162.   For dCount := 1 to (MaxDatabases-1) do
  163.     DBArray[dCount].Free;
  164. end;
  165.  
  166. procedure TForm1.sbBuildSQLClick(Sender: TObject);
  167. begin
  168.   Query1.Active := False;
  169.  
  170.   {Set SQLBuilder options to match options selected in checkboxes}
  171.   SetBuilderOptions;
  172.  
  173.   {Build the query }
  174.   If SQLBuilder1.Execute then
  175.   begin
  176.     memo1.lines.Assign(SQLBuilder1.SQL);
  177.     PrepareAndRunQuery;
  178.   end;
  179. end;
  180.  
  181. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  182. begin
  183.   CanClose := (MessageDlg('Leave SQLBuilder Demonstration ?', mtConfirmation, [mbYes, mbNo], 0) = mrYes);
  184. end;
  185.  
  186. procedure TForm1.SpeedButton1Click(Sender: TObject);
  187. begin
  188.   Close;
  189. end;
  190.  
  191. end.
  192.